package com.hunny.singleton;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 登记式单例模式
 * @author Create By Cb
 * @date 2022/04/25
 * @since 2022/4/25 10:05
 * <p>
 * 并非单利模式独有的写法
 * </p>
 */
@Slf4j
public class RegisterSingleton {
    /**
     * 单例图
     */
    private static Map<Class<?>, BaseSingleton> singletonMap;

    static {
        singletonMap = new HashMap<>();
        singletonMap.put(IdlerSingleton.class, IdlerSingleton.getInstance());
        singletonMap.put(LazyHolderSingleton.class, LazyHolderSingleton.getInstance());
        singletonMap.put(HungryBaseSingleton.class, HungryBaseSingleton.getInstance());
    }

    /**
     * 获得实例
     *
     * @param clazz clazz
     * @return {@link BaseSingleton}
     */
    public static BaseSingleton getInstance(Class<?> clazz) {
        if (clazz != null) {
            return singletonMap.get(clazz);
        }
        log.error("没有找到对应的单例类");
        return null;
    }
}